package com.guijq.codesegment.tree;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;


@Data
public class TreeNodeBo {
    //树节点ID
    private Integer nodeId;
    //树节点名称
    private String nodeName;
    //父节点ID
    private Integer parentNodeId;

    private Integer parentLevelNodeId;
    //节点在树中的排序号
//    private String nodeId;
    //节点所在的层级
    private int level;
    private ITreeNode object;
    private TreeNodeBo parent;

    //当前节点的二子节点
    private List<TreeNodeBo> children = new ArrayList<TreeNodeBo>();
    //当前节点的子孙节点
    private List<TreeNodeBo> allChildren = new ArrayList<TreeNodeBo>();

    public TreeNodeBo() {

    }

    public TreeNodeBo(ITreeNode obj) {
        this.nodeId = obj.getNodeId();
        this.nodeName = obj.getNodeName();
        this.parentNodeId = obj.getNodeParentId();
        this.object = obj;
        this.level = obj.getLevel();
        this.parentLevelNodeId = obj.getNodeParentLevelId();
    }

    public void addChild(TreeNodeBo treeNodeBo){
        this.children.add(treeNodeBo);
        //同时更新 allChildren
        if (!this.allChildren.isEmpty()) {
            this.allChildren.add(treeNodeBo);
        }
    }
    public void removeChild(TreeNodeBo treeNodeBo){
        this.children.remove(treeNodeBo);
        //更新all children
        if (!this.allChildren.isEmpty()) {
            this.allChildren.remove(treeNodeBo);
        }
    }


    public List<TreeNodeBo> getAllChildren() {
        if(this.allChildren.isEmpty()){
            for(TreeNodeBo treeNodeBo : this.children){
                this.allChildren.add(treeNodeBo);
                this.allChildren.addAll(treeNodeBo.getAllChildren());
            }
        }
        return this.allChildren;
    }
}
